﻿/*
 * ShpSelectCommand.prepareSpatialFilters
 * 
 * initial implementation : 20.12.2008
 *
 * Copyright (c) Adam Sida, asida@centrum.cz.
 * All rights reserved.
 * 
 * for license see Petra.Gis.License.txt
 */
using System;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using Petra.Gis.DataServices;
using Petra.Gis.Geometry;
using Petra.Gis.Geometry.Support;
using Petra.Gis.DataServices.Metadata;


namespace Petra.Gis.DataServices.SHP
{
  public partial class ShpSelectCommand : SpatialDbCommand
  {


    protected void prepareSpatialFilters()
    {


      if (!this.Parameters.ContainsKey("Petra.Gis.DataServices.FeatureSpatialFilter"))
        return;

      //Dictionary<string, BoundingBox> spatialFilters = (Dictionary<string, BoundingBox>)this.Parameters["Petra.Gis.DataServices.FeatureSpatialFilter"].Value;
      FeatureSpatialFilter spatialFilters = (FeatureSpatialFilter)this.Parameters["Petra.Gis.DataServices.FeatureSpatialFilter"].Value;

      //todo:resolve unspecified field name
      //when feature has only one geom field no error
      //when has multiple throw error
      //if (spatialFilters.SpatialFieldName == "" || spatialFilters.SpatialFieldName == null)

      //if (Metadata == null)
      //  Metadata = getFeatureMetadata();

      if (Metadata == null)
        throw new ApplicationException(@"no metadata available for this command, 
                                         cant prepare spatial filters");

        // get tablename from command.CommandText
        FeatureQualifier tq = new FeatureQualifier()
        {
          Name = this.CommandText
        };

        DataTable t = _connection.GetSpatialSchema(SpatialSchemaTables.SpatialIndexes, new string[]{ tq.Catalog, tq.Schema, tq.Name, spatialFilters.SpatialFieldName, null});

        if(t.Rows.Count == 0)
          return; //no spatial filter available

        int i = 0;
        //for(int i = 0 ; i< t.Rows.Count;i++)
        //{
        if(t.Rows[i]["SPATIAL_INDEX_TYPE"].ToString().ToLower() == "none")
        {
          return; //no index for this table column
        }
        //else if(t.Rows[i]["SPATIAL_INDEX_TYPE"].ToString().ToLower() == "quadtreeindex" &&
        //spatialFilters.ContainsKey(t.Rows[i]["COLUMN_NAME"].ToString()))
        //{
        //  prepareQTreeSpatialFilter(tq, t.Rows[i]["COLUMN_NAME"].ToString());
        //}

        // prepare and build real sqlParameters...
        //prepareSpatialFilterParameter("@CoarseSpatialFilter_xlo", spatialFilters[t.Rows[i]["COLUMN_NAME"].ToString()].Left);
        //prepareSpatialFilterParameter("@CoarseSpatialFilter_ylo", spatialFilters[t.Rows[i]["COLUMN_NAME"].ToString()].Bottom);
        //prepareSpatialFilterParameter("@CoarseSpatialFilter_xhi", spatialFilters[t.Rows[i]["COLUMN_NAME"].ToString()].Right);
        //prepareSpatialFilterParameter("@CoarseSpatialFilter_yhi", spatialFilters[t.Rows[i]["COLUMN_NAME"].ToString()].Top);          
        //}
    }





    protected void prepareQTreeSpatialFilter(FeatureQualifier tq, string geometryColumnName)
    {


    }





    protected void prepareSpatialFilterParameter(string parameterName, double parameterValue)
    {


    }




  }
}
